package com.zn.web.controler;


import com.zn.entity.OptionMarketDataField;
import com.zn.service.MdInfoService;
import com.zn.vo.Message;
import com.zn.vo.OptionSymbol;
import com.zn.web.vo.TradingVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mdInfo")
public class MdInfoController {
    private static final Logger LOGGER = LoggerFactory.getLogger(MdInfoController.class);
    @Autowired
    private MdInfoService mdInfoService;

    /**
     * 进入APP第一次获取数据
     * @return
     */
    @RequestMapping(value = "/getTargetList",method = RequestMethod.POST)
    public ResponseEntity<Message> getTargetList() {
        try {
            Map<String, List<?>> map = mdInfoService.getTargetList();
            return ResponseEntity.ok(Message.successData(map));
        }catch (Exception ex){
            LOGGER.error("getTargetList........."+ex);
        }
        return ResponseEntity.ok(Message.SYS_ERROR);
    }

    /**
     * 根据条件查询首页数据
     * @param record
     * @return
     */
    @RequestMapping(value = "/findTargetList",method = RequestMethod.POST)
    public ResponseEntity<Message> findTargetList(@RequestBody TradingVo record) {
        try {
            if (StringUtils.isBlank(record.getTradingGoodsCode())){
                return ResponseEntity.ok(Message.ARGS_ERROR);
            }
            Map<String, List<?>> map = mdInfoService.findTargetList(record);
            return ResponseEntity.ok(Message.successData(map));
        }catch (Exception ex){
            LOGGER.error("findTargetList........."+ex);
        }
        return ResponseEntity.ok(Message.SYS_ERROR);
    }

    /**
     * 获取合约详情
     * @param record
     * @return
     */
//    @RequestMapping(value = "/getContract",method = RequestMethod.POST)
//    public ResponseEntity<Message> getContract(@RequestBody TradingVo record) {
//        try {
//            if ("".equals(record.getInstrumentCode()) || record.getInstrumentCode() == null){
//                return ResponseEntity.ok(Message.ARGS_ERROR);
//            }
//            OptionMarketDataField optionMarketDataField = mdInfoService.getTargetContract(record);
//            return ResponseEntity.ok(Message.successData(optionMarketDataField));
//        }catch (Exception ex){
//            System.out.println(ex.getMessage());
//        }
//        return ResponseEntity.ok(Message.SYS_ERROR);
//    }

    /**
     * 获取行情初始化数据
     * @param record
     * @return
     */
    @RequestMapping(value = "/getQuotation",method = RequestMethod.POST)
    public ResponseEntity<Message> getQuotation(@RequestBody TradingVo record) {
        try {
            if (StringUtils.isBlank(record.getInstrumentCode())){
                return ResponseEntity.ok(Message.ARGS_ERROR);
            }
            Object minLine = mdInfoService.getSinaMinLine(record);
            return ResponseEntity.ok(Message.successData(minLine));
        }catch (Exception ex){
            LOGGER.error("getQuotation........."+ex);
        }
        return ResponseEntity.ok(Message.SYS_ERROR);
    }

    /**
     * 获取所有合约名称信息
     * @return
     */
    @RequestMapping(value = "/getContractList",method = RequestMethod.POST)
    public ResponseEntity<Message> getContractList() {
        try {
            List<OptionSymbol> optionSymbolList = mdInfoService.getContractList();
            return ResponseEntity.ok(Message.successData(optionSymbolList));
        }catch (Exception ex){
            LOGGER.error("getContractList........."+ex);
        }
        return ResponseEntity.ok(Message.SYS_ERROR);
    }

    /**
     * 根据合约ID查询合约信息
     * @param record
     * @return
     */
    @RequestMapping(value = "/findContract",method = RequestMethod.POST)
    public ResponseEntity<Message> findContract(@RequestBody TradingVo record) {
        try {
            if (StringUtils.isBlank(record.getInstrumentCode())){
                return ResponseEntity.ok(Message.ARGS_ERROR);
            }
            OptionMarketDataField optionMarketDataField = mdInfoService.getTargetContract(record);
            return ResponseEntity.ok(Message.successData(optionMarketDataField));
        }catch (Exception ex){
            LOGGER.error("findContract........."+ex);
        }
        return ResponseEntity.ok(Message.SYS_ERROR);
    }

}